package com.glasses.orm.base;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glasses.orm.base.Ex.BaseEx;
import com.glasses.orm.config.ProviderName;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.reflections.Reflections;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * 服务基类
 */
public abstract class BaseService extends BaseEx {
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
    private TransactionFactory transactionFactory = new JdbcTransactionFactory();

    private final static Map<String, Class<?>> providers = new HashMap<>();

    //缓存全部的字典信息
    static {
        Set<Class<?>> classSet = new Reflections("com.glasses.orm.dictionary").getTypesAnnotatedWith(ProviderName.class);
        for (Class c : classSet) {
            providers.put(((ProviderName) c.getAnnotation(ProviderName.class)).name(), c);
        }
    }

    public JSONObject getEnumType(Map map) throws Exception {
        if (map.get("type") == null || "".equals(map.get("type").toString()))
            return this.codeMess(eCode, "枚举类型实例不能为空");
        String[] typeArr = map.get("type").toString().split(",");
        JSONObject jsonArr = new JSONObject();
        for (String type : typeArr) {
            Class<?> clazz = providers.get(type);
            if (clazz == null)
                return this.codeMess(eCode, "枚举类型有误");
            Method m = clazz.getDeclaredMethod("values");
            Enum[] enumArr = (Enum[]) m.invoke(Enum.class);
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < enumArr.length; i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("val", String.valueOf(enumArr[i]));
                jsonObject.put("text", enumArr[i]);
                jsonArray.add(jsonObject);
            }
            jsonArr.put(type, jsonArray);
        }
        return this.codeData(jsonArr);
    }

    public JSONObject getEnumTypeSingle(Map map) throws Exception {
        if (map.get("type") == null || "".equals(map.get("type").toString()))
            return this.codeMess(eCode, "枚举类型实例不能为空");
        String[] typeArr = map.get("type").toString().split(",");
        JSONObject jsonArr = new JSONObject();
        for (String type : typeArr) {
            JSONObject jsonObject = new JSONObject();
            Class<?> clazz = providers.get(type);
            if (clazz == null)
                return this.codeMess(eCode, "枚举类型有误");
            Method m = clazz.getDeclaredMethod("values");
            Enum[] enumArr = (Enum[]) m.invoke(Enum.class);
            for (int i = 0; i < enumArr.length; i++) {
                jsonObject.put(String.valueOf(enumArr[i]), enumArr[i]);
            }
            jsonArr.put(type, jsonObject);
        }
        return this.codeData(jsonArr);
    }
    /**
     * 获得一个事物
     *
     * @return
     */
    public Transaction getTramsaction() {
        return transactionFactory.newTransaction(sqlSessionTemplate.getConnection());
    }

    /**
     * 手动回滚事务
     */
    public void rollBack() {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }

    /**
     * 字典-->具体名称
     *
     * @return
     */
    public String getOrdinalHandlerVal(Object object, String property, String value) {
        return null;
    }

    /**
     * 获得当前方法名
     *
     * @return
     */
    public String getMethodName() {
        return Thread.currentThread().getStackTrace()[1].getMethodName();
    }

}
